//
//  SecondViewController.m
//  地址选择器
//
//  Created by admin on 16/6/15.
//  Copyright © 2016年 sigxui-001. All rights reserved.
//

#import "SecondViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "CitiesView.h"
#import "ZXAreaModel.h"

@interface SecondViewController ()<UITableViewDataSource,UITableViewDelegate>


@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <ZXProvModel *>*provinces;


@end

@implementation SecondViewController

- (UITableView *)tableView {
    if(!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tag = 1;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
        _tableView.rowHeight = 40;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择地区";
    [self.view addSubview:self.tableView];

    // Do any additional setup after loading the view from its nib.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    WeakSelf(WSelf);
    [[ZXHTTPManager sharedManager]getProvInfoCompleted:^(NSDictionary *jsonDict, NSError *error) {
       
//        NSLog(@"%@",jsonDict);
        WSelf.provinces = [ZXProvModel mj_objectArrayWithKeyValuesArray:jsonDict[@"result"]];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.provinces.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    // NSLog(@"%@",provinces);
    cell.textLabel.text = self.provinces[indexPath.row].provName;
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CitiesView *cityView = [[CitiesView alloc]init];
    cityView.provId = self.provinces[indexPath.row].provId;
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    cityView.blockCity = ^(NSMutableDictionary *dict){
        if (dict.count == 4) {
            
            dic[@"regionName"] = dict[@"regionName"];
            dic[@"regionCode"] = dict[@"regionCode"];
            dic[@"cityName"] = dict[@"cityName"];
            dic[@"cityCode"] = dict[@"cityCode"];
            dic[@"province"] = _provinces[indexPath.row].provName;
            dic[@"provinceCode"] = _provinces[indexPath.row].provCode;

        }
        else if(dict.count == 2){

            
        }
        
        [NSString stringWithFormat:@"%@ %@",_provinces[indexPath.row].provName,dict];

        self.blockAddress((NSMutableDictionary *)dic);
        [self.navigationController popViewControllerAnimated:NO];
    };
    [self.view addSubview:cityView];
}
@end
